package redisclient

import (
	"gitee.com/lsy007/redisclient/model"
	"github.com/pquerna/ffjson/ffjson"
)

// PFAdd(key string, els ...interface{}) (count int64, err error)
// PFCount(keys ...string) (count int64, err error)
// PFMerge(dest string, keys ...string) (status string, err error)

// 将任意数量的元素添加到指定的 HyperLogLog 里面
func (c *RedisClient) PFAdd(key string, els ...interface{}) (count int64, err error) {
	var bytes []byte
	if bytes, err = ffjson.Marshal(els); err != nil {
		return
	}
	args := &model.RedisArgsModel{Key: key, Bytes: bytes}
	response, err = c.Request("PFAdd", "HyperLogLog", args)
	return response.Int, err
}

// 单个键时， 返回储存在给定键的 HyperLogLog 的近似基数， 如果键不存在， 那么返回 0;多个键时，返回所有给定 HyperLogLog 的并集的近似基数
func (c *RedisClient) PFCount(keys ...string) (count int64, err error) {
	args := &model.RedisArgsModel{Keys: keys}
	response, err = c.Request("PFCount", "HyperLogLog", args)
	return response.Int, err
}

// 将多个 HyperLogLog 合并（merge）为一个 HyperLogLog ， 合并后的 HyperLogLog 的基数接近于所有输入 HyperLogLog 的可见集合（observed set）的并集
func (c *RedisClient) PFMerge(dest string, keys ...string) (status string, err error) {
	args := &model.RedisArgsModel{Destination: dest, Keys: keys}
	response, err = c.Request("PFMerge", "HyperLogLog", args)
	return response.String, err
}
